<template>
    <bmContainer :touchBarShow="true" touchBarColor="#fafafa">
        <div class="flex">
            <div class="app-wrapper flex">
                <embed v-for="(item,index) in items" :src="item.src" type="weex" class="content" :style="{ visibility: item.visibility }"></embed>
                <tab-bar @tabTo="onTabTo" :items="items"></tab-bar>
                <div class="touch-bar"></div>
            </div>
        </div>
    </bmContainer>
</template>
<script>
var amap = weex.requireModule('amap')
var axios = weex.requireModule('bmAxios')
var globalEvent = weex.requireModule('globalEvent')
var hasNav = weex.requireModule('hasNav')
import util from './utils/util';
import tabBar from './common/tabBar';
import bmContainer from '../components/bmContainer'
import { Utils} from 'weex-ui';
import { tabConfig } from './config'
export default {
    data(){
        return{
            token:'',
            maxHomeBackTriggerTimes:3,
            curHomeBackTriggerTimes:1,
        }
    },
    bmRouter: {
        viewWillAppear() {
            console.log('home-index-viewWillAppear');
        },
        viewWillBackAppear(params) {
            console.log('home-index-viewWillBackAppear');
        }
    },
    components: {
        'tab-bar': tabBar,
        bmContainer,
        Utils,
    },
    methods:{
        getAddress(locationLng,locationLat){
            // 示例
            let data = {}
            let _this = this
            let loc = locationLng+','+locationLat
            data.location = loc
            data.key = 'c1f73016285c30b57fda31598f5757c2'
            axios.fetch({
                method: 'GET',
                url: 'https://restapi.amap.com/v3/geocode/regeo',
                header: {},
                data: data,
                timeout: 3000
            }, function(resData){
                if(resData.data.status == "1"){
                    _this.$storage.set('address',resData.data.regeocode.formatted_address)
                    _this.$notice.toast({
                        message: resData.data.regeocode.formatted_address
                    })
                }else{
                    _this.$notice.toast({
                        message: '定位失败'
                    })
                }                        
                
            })
        },
        androidFinishApp(){
            globalEvent.addEventListener('homeBack', options => {
                (this.curHomeBackTriggerTimes === this.maxHomeBackTriggerTimes) && this.$router.finish()
                this.curHomeBackTriggerTimes++
                this.$notice.toast({ message: `点击返回${this.maxHomeBackTriggerTimes}次之后，会关闭应用，当前点击第${this.curHomeBackTriggerTimes}次` })
            })
        }
    },
    created() {
         // 获取token
        this.token = this.$storage.getSync('token')
        if(!this.token){
            this.$router.setHomePage('/pages/login.js')
        }
        util.initIconFont()
        // hasNav.hasNav(function(resData){
        //     if(resData.result == 'success'){
        //         console.log(resData.data);
        //     }else{
        //     }
        // })
        
        //获取定位
        // this.$geo.get().then(data => {
        //     //获取行政位置
        //     var locationLng = data.locationLng
        //     var locationLat = data.locationLat
        //     // var locationLng = '120.246842'
        //     // var locationLat = '31.546071'
        //     this.getAddress(locationLng,locationLat)
        // }, error => {
        //     this.$notice.toast({
        //         message: '定位失败'
        //     })
        // })
        amap.initAmap('e6a87676260d70740d8303b686dfd056')
        if(Utils.env.isAndroid()){
            var id = 'test'
            let _this = this
            amap.getUserLocation("test",function (resData) {
                if(resData.result == 'success'){
                    _this.$storage.set('address',resData.data.position[2])
                }else{
                    _this.$notice.toast({
                        message: '定位失败'
                    })
                }
            });  
        }else{
            this.$geo.get().then(data => {
                //获取行政位置
                var locationLng = data.locationLng
                var locationLat = data.locationLat
                // var locationLng = '120.246842'
                // var locationLat = '31.546071'
                this.getAddress(locationLng,locationLat)
            }, error => {
                this.$notice.toast({
                    message: '定位失败'
                })
            })
        }
        this.androidFinishApp()
        
    },
    data() {
        return {
            items: tabConfig,
        }
    },
    methods: {
        onTabTo(_result) {
            let _key = _result.data.key || '';
            this.items.forEach((val) => {
                if (val.key === _key) {
                    val.visibility = 'visible'
                    return
                }
                val.visibility = 'hidden'
            })
        },
    }
}
</script>
<style>
body {
    margin: 0;
    padding: 0;
    background-color: #f4f4f4;
    color: #333;
}
</style>
<style lang="sass" scoped>
@import 'src/js/css/core/base.scss';
.iconfont {
    font-family: iconfont;
}

.content {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin-top: 0;
    /*margin-bottom: 100;*/
}

.app-wrapper {
    background-color: #f4f4f4;
}

.r-box {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

.iconfont {
    font-family: iconfont;
}

.wrapper {}

.main-list {
    position: fixed;
    top: 168px;
    bottom: 90px;
    left: 0;
    right: 0;
    /*margin-top: 167px;*/
    /*margin-bottom: 90px;*/
}

.cell-button {
    padding-bottom: 18px;
}

.slogan {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    background-color: #fff;
}

.i-slg {
    height: 66px;
    font-size: 26px;
    padding-top: 16px;
    flex: 1;
    text-align: center;
    color: #b4282d;
}
</style>